<?php

class ForbidDeviceModel
{
    private $forbidDeviceKeyPrefix  =   'FD/';
    private $forbidLoginKeyPrefix   =   'FDL/';

    public function forbidDevice($info)/*{{{*/
    {
        if (empty($info['uid']) &&  empty($info['deviceSN'])    &&  empty($info['account']))
        {
            AC::$errorNo    =   'FDM001';
            return  FALSE;
        }
        if ('sync www.fenfenriji.com'   ==  $info['deviceSN'])  return  TRUE;

        $data   =   array(
                        'uid'       =>  $info['uid'],
                        'account'   =>  $info['account'],
                        'nickname'  =>  $info['nickname'],
                        'deviceSN'  =>  $info['deviceSN'],
                        'time'      =>  AC::SERVER_TIME,
                        'lastTime'  =>  AC::SERVER_TIME,
                    );
        $key    =   $this->forbidDeviceKeyPrefix. 0;
        $result =   Storage::addData($key, $data);

        return  $result;
    }/*}}}*/
    
    public function unForbidDevice($id)/*{{{*/
    {
    	if (empty($id))
    	{
    		AC::$errorNo    =   'FDM002';
    		return  FALSE;
    	}
    
    	$where  =   array('id'  => $id);
    	$key    =   $this->forbidDeviceKeyPrefix. 0;
    	$result =  Storage::delConditionData($key, $where);
    
    	return  $result;
    }/*}}}*/

    public function unchain($info)/*{{{*/
    {
        if (empty($info['deviceSN']))
        {
            AC::$errorNo    =   'FDM002';
            return  FALSE;
        }

        $data   =   array('status'      =>  0);
        $where  =   array('deviceSN'    =>  $info['deviceSN']);
        $key    =   $this->forbidDeviceKeyPrefix. 0;
        $result =   Storage::setConditionData($key, $data, $where);

        return  $result;
    }/*}}}*/

    public function getForbidDeviceInfoByUid($uid)/*{{{*/
    {
        if (empty($uid))
        {
            AC::$errorNo    =   'FDM002';
            return  FALSE;
        }

        $where  =   array('uid'    =>  $uid);
        $key    =   $this->forbidDeviceKeyPrefix. 0;
        $result =   Storage::getConditionData($key, $where);

        return  $result;
    }/*}}}*/

    public function getForbidDeviceInfo($info)/*{{{*/
    {
        if (empty($info['deviceSN']))
        {
            AC::$errorNo    =   'FDM002';
            return  FALSE;
        }

        $where  =   array('deviceSN'    =>  $info['deviceSN']);
        $key    =   $this->forbidDeviceKeyPrefix. 0;
        $result =   Storage::getConditionData($key, $where);

        return  $result;
    }/*}}}*/
    
    /**
     * 封禁账户时将登录信息保存至数据库
     * @param unknown $info
     * @return boolean|Ambigous <unknown, boolean>
     */
    public function addForbidDeviceLogin($info){
        if (empty($info)){
            return TRUE;
        }
        
        if (!empty($info['account'])){
            $info['openUid'] = $info['account'];
        }
        
        if (!empty($info['loginDevice'])){
            $info['registerDevice']=  $info['loginDevice'];
        }
        
        
        $data    =    array();
        $data['open_uid']            =    $info['openUid'];
        isset($info['platform'])         &&   $data['platform']        =   $info['platform'];
        isset($info['nickname'])         &&   $data['nickname']        =   $info['nickname'];
        isset($info['openToken'])        &&   $data['open_token']      =   $info['openToken'];
        isset($info['registerChannel'])  &&   $data['register_channel']=   $info['registerChannel'];
        isset($info['registerDevice'])   &&   $data['register_device'] =   $info['registerDevice'];
        isset($info['deviceSN'])         &&   $data['device_sn']       =   $info['deviceSN'];
        isset($info['sex'])              &&   $data['sex']             =   $info['sex'];
        isset($info['city'])             &&   $data['city']            =   $info['city'];
        isset($info['tel'])              &&   $data['tel']             =   $info['tel'];
        
        $data['time']                =    AC::SERVER_TIME;
        
        $key    =   $this->forbidLoginKeyPrefix. 0;
        $result =   Storage::addData($key, $data);
        return  $result;
    }
    
    /**
     * 获取被封禁用户的登录信息
     * 使用长度分页
     * @param unknown $info
     * @return Ambigous <boolean, multitype:, unknown>
     */
    public function getForbidDeviceLogin($info){
        
        $start    =    0;
        $length   =    AC::LIKE_LIST_DISPLAY_LEN;
        isset($info['start'])    && $start    =    intval($info['start']);
        isset($info['length'])   && $length   =    intval($info['length']);
        
        if ($start < 0){
            $start = 0;
        }
        
        $orderBy  =    ' id DESC ';
        $key    =   $this->forbidLoginKeyPrefix. 0;
        $result =   Storage::getManyData($key, $orderBy, $start, $length);
        
        return  $result;
        
    }
    
}

